package com.xxwy.netty;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author wy
 * @date 2020/3/31 17:58
 */
public class ConnectionCountHandler extends ChannelInboundHandlerAdapter {

  AtomicInteger nConnection = new AtomicInteger();

  public ConnectionCountHandler( ) {
    Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(()->{
      System.out.println("connetion:" + nConnection.get());
    },0,2, TimeUnit.SECONDS);
  }

  @Override
  public void channelActive(ChannelHandlerContext ctx) throws Exception {
    nConnection.incrementAndGet();
  }

  @Override
  public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    nConnection.decrementAndGet();
  }
}
